
#import "WWTextField.h"
#import "WWTextFieldCell.h"

#import "avoidStupidWarnings.h"

@implementation WWTextField

static id myStoredCellClass;


+ initialize 
{ 
  [WWTextField setVersion:1]; 

  if (self == [WWTextField class])
  {  myStoredCellClass = [WWTextFieldCell class];
  }

  return self; 
}
//
+ setCellClass:classID
{
  myStoredCellClass = classID;
  return self;
}
////////////////////////////////////////////////////////
- initFrame:(const NXRect *)frameRect
{
  id          oldCell;
  const char  *WDRAG_PBTYPE[] = { NXFilenamePboardType, NULL};


  [super initFrame:frameRect];
  oldCell = [self setCell:[[WWTextFieldCell alloc] init]];
  [oldCell free];
  [self registerForDraggedTypes:WDRAG_PBTYPE count:1];
  [[self cell] setScrollable:YES];
  return self;
}
//
- init
{
  id  oldCell;


  [super init];
  oldCell = [self setCell:[[myStoredCellClass alloc] init]];
  [oldCell free];
  return self;
}

- awake
{
  const char  *WDRAG_PBTYPE[] = { NXFilenamePboardType, NULL};


  [super awake];
  [self registerForDraggedTypes:WDRAG_PBTYPE count:1];

  return self;
}

- free
{
   const char *fileType[] = { "" };
   Pasteboard *pboard = [Pasteboard newName:NXDragPboard];

   // why doesn't this work?
   [pboard declareTypes:fileType num:0 owner:nil];
   [self unregisterDraggedTypes];

   return [super free];
}

- sendAction:(SEL)theAction to:theTarget
{
  if ([cell respondsTo:@selector(updateInterp)])
  {  [cell updateInterp];
  }

  return [super sendAction:theAction to:theTarget];
}

// need to be able to see the cell's tclVar...
- setTclVar:(const char *)str { return [[self cell] setTclVar:str]; }
- (const char *)tclVar { return [[self cell] tclVar]; }

// need to be able to see the cell's controlString...
- setControlString:(const char *)str { return [[self cell] setControlString:str]; }
- (const char *)controlString { return [[self cell] controlString]; }

// need to be able to see the cell's tclExpression...
- setTclExpression:(const char *)str { return [[self cell] setTclExpression:str]; }
- (const char *)tclExpression { return [[self cell] tclExpression]; }

- setInterp:newInterp {  return [[self cell] setInterp:newInterp]; }
- evaluateSelf { return [[self cell] evaluateSelf]; }

////////////////////////////////////////////////////////////////////
//			Font stuff
///////////////////////////////////////////////////////////////////

- changeFont:sender
{
  [cell setFont:[sender convertFont:[cell font]]];
  if ([cell respondsTo:@selector(updateInterp)])
  {  [cell updateInterp];
  }
  return self;
}


- changeIBFont:sender
{
  [cell setFont:[sender convertFont:[cell font]]];
  if ([cell respondsTo:@selector(updateInterp)])
  {  [cell updateInterp];
  }
  return self;
}


////////////////////////////////////////////////////////////////////
//			Dragging stuff
///////////////////////////////////////////////////////////////////


- (NXDragOperation)draggingEntered:(id <NXDraggingInfo>)sender
{
    Pasteboard    *pboard;
    const NXAtom  *theType;


    pboard = [sender draggingPasteboard];
    theType = [pboard types];

    if (*theType != NXFilenamePboardType)
    {  fprintf(stderr, "Right type: acknowledging dragging starting...\n");
       fflush(stderr);
       return NX_DragOperationCopy;
    }
    fprintf(stderr, "Wrong type: unable to acknowledge dragging starting.\n");
    fflush(stderr);

    return NX_DragOperationNone;
}

- (NXDragOperation)draggingUpdated:(id <NXDraggingInfo>)sender
{
	return NX_DragOperationCopy;
}


- (BOOL)performDragOperation:(id <NXDraggingInfo>)sender
{
    Pasteboard    *pboard;
    char          *argv;
    int           len;
    const NXAtom  *theType;


    pboard = [sender draggingPasteboard];
    theType = [pboard types];

    if (*theType != NXFilenamePboardType)
    {  if ([pboard readType:NXFilenamePboardType data:&argv length:&len] == nil)
       {  fprintf(stderr, "unable to read filename from pasteboard.\n"); fflush(stderr);
	  return NO;
       }
       else
       {  [self setStringValue:(const char *)argv];
	  [self display];
          return YES;
       }
    }
    else
    {  fprintf(stderr, "The type of the dragged object isn't a filename.\n"); fflush(stderr);
       return NO;
    }
}

// IB stuff
- (const char *)getInspectorClassName 
{  
   NXEvent  *event = [NXApp currentEvent];

   if (event->flags & NX_ALTERNATEMASK)
   {  return [super getInspectorClassName];
   }
   
   return "WWTextFieldIBInspector"; 
}

- write:(NXTypedStream *)stream 
{
   [super write:stream];
   return self;
}
//
- read:(NXTypedStream *)stream 
{
   int version;

   [super read:stream];

   version = NXTypedStreamClassVersion(stream, "WWTextField");
   if (version == 1)
   {  
   }
   return self;
}

@end
